home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld: Complete Mac Interactive
/
Macworld Complete Mac Interactive CD)(1994).iso
/
The Best of BMUG
/
Utilities
/
Text and Speech
/
Alpha.5.76
/
Tcl
/
SystemCode
/
c.tcl
< prev
next >
Wrap
Text File
|
1994-03-08
|
4KB
|
170 lines
#=============================================================================
# "Electric" C functions.
#=============================================================================
# returns the indent string of the line named by 'pos'
proc indentString pos {
set start [lineStart $pos]
set end [nextLineStart $pos]
set text [getText $start $end]
for {set i 0} {1} {incr i} {
set c [string index $text $i]
if {($c != "\ ") && ($c != "\t")} then {
return [string range $text 0 [expr $i-1]]
}
}
return
}
# Brace on new line, same indentation. Insert on another new line, indented in.
# First, see if we are on new line.
proc electricCLeft {} {
global elecLBrace
deleteText [getPos] [selEnd]
if {$elecLBrace == "0"} then {
insertText "\{"
return
}
set pos [getPos]
set start [lineStart $pos]
set text [getText $start $pos]
for {set i $start} {$i < $pos} {incr i} {
set c [lookAt $i]
if {($c != "\ ") && ($c != "\t")} then {
set indentation [getText $start $i]
insertText " \{\r" $indentation "\t"
return
}
}
set indentation [getText $start $pos]
insertText "\{\r" $indentation "\t"
}
bind '\{' <s> electricCLeft
# Brace on new line, immediate carriage return
proc electricCRight {} {
global elecRBrace
deleteText [getPos] [selEnd]
if {$elecRBrace == "0"} then {
insertText "\}"
catch {blink [matchIt "\}" [expr [getPos]-2]]}
return
}
set pos [getPos]
set start [lineStart $pos]
if {[catch {matchIt "\}" [expr $pos-1]} matched]} {
beep
return
}
set text [getText [lineStart $matched] $matched]
regexp {^[ ]*} $text indentation
for {set i $start} {$i < $pos} {incr i} {
set c [lookAt $i]
if {($c != "\ ") && ($c != "\t")} then {
insertText "\r" $indentation "\}\r" $indentation
blink $matched
return
}
}
set text [set indentation]\}\r$indentation
replaceText $start $pos $text
goto [expr {$start + [string length $text]}]
blink [matchIt "\}" [expr $start-2]]
}
bind '\}' <s> electricCRight
# Brace on new line, immediate carriage return. We don't do our electric stuff
# if we are in the middle of a for statement.
proc electricCSemi {} {
global electricSemi
deleteText [getPos] [selEnd]
if {$electricSemi == "0"} then {
insertText ";"
return
}
set pos [getPos]
set start [lineStart $pos]
set text [getText $start $pos]
if {[string first "for" $text] != "-1"} {
set lefts 0
set rights 0
set len [string length $text]
for {set i 0} {$i < $len} {incr i} {
case [string index $text $i] in {
"(" { incr lefts }
")" { incr rights }
}
}
global globs
set globs [list $lefts $rights $len]
if {$lefts != $rights} {
insertText ";"
return
}
}
insertText ";\r" [indentString $pos]
}
bind '\;' electricCSemi
# 'C' programming mode
proc setCMode {} {
global thinkMenu
changeMode "C"
uplevel #0 {
set prefixString "//"
set elecLBrace 1
set elecRBrace 1
set electricSemi 1
set wordWrap 0
set funcExpr {^[^ \t\(#\r/@].*\(.*\)$}
set sortedIsDefault 1
# set wordBreakPreface {[^a-zA-Z0-9]}
# set wordBreak {[a-zA-Z0-9]+}
set wordBreakPreface {[^a-zA-Z0-9_]}
set wordBreak {[a-zA-Z0-9_]+}
}
insertMenu $thinkMenu
}
proc setC++Mode {} {
global wordBreakPreface wordBreak thinkMenu
changeMode "C++"
uplevel #0 {
set prefixString "//"
set elecLBrace 1
set elecRBrace 1
set electricSemi 1
set wordWrap 0
set funcExpr {^([^ \t\(#\r/@].*[ \t]+)?([A-Za-z0-9:~_]+)[ \t\r]*\(.*\)?$}
set funcPar 2
set sortedIsDefault 1
set wordBreakPreface {[^a-zA-Z0-9_]}
set wordBreak {[a-zA-Z0-9_]+}
}
insertMenu $thinkMenu
}
proc colorCComment {} {
insertText {/}
if {([set pos [getPos]] >= 3) && ([lookAt [expr $pos - 2]] == "*")} {
if {![catch {search -f 0 -r 0 -i 0 {/*} $pos} res]} {
colorize [lindex $res 0] $pos red
}
}
}
bind '/' colorCComment
source "$HOME:Tcl:SystemCode:think.tcl"